//
//  MCOptionsSelectView.m
//  DemoTest
//
//  Created by Anson on 14-11-27.
//  Copyright (c) 2014年 Anson. All rights reserved.
//

#import "MCOptionsSelectView.h"

@implementation MCOptionSelectViewItem
- (instancetype)initWithTitle:(NSString *)title value:(NSString *)val {
    self = [super init];
    if (self) {
        _title = title;
        _value = val;
    }
    return self;
}
@end

@interface MCOptionsSelectView () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *optionsGridView;
@property (strong, nonatomic) NSMutableArray *optionItemList;
@property (strong, nonatomic) NSMutableArray *optionItemSelectedList;
@property (strong, nonatomic) NSIndexPath *preSelectedIndexPath;
@end

@implementation MCOptionsSelectView

- (instancetype)initWithFrame:(CGRect)frame delegate:(id<MCOptionsSelectViewDelegete>)delegate {
    self = [super init];
    if (self) {
        _delegate = delegate;
        _columns = 2;
        _multiSelect = NO;
        [self initOptionsView:frame];
    }
    return self;
}

- (void)initOptionsView:(CGRect)frame {
    
    CGSize sizeView = [UIScreen mainScreen].bounds.size;
    
    self.optionsGridView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    //self.optionsGridView.rowHeight = 50.0;
    self.optionsGridView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.optionsGridView.delegate = self;
    self.optionsGridView.dataSource = self;
    self.optionsGridView.backgroundColor = [UIColor whiteColor];
}

- (void)showInView:(UIView *)view {
    
    UIView *presentingView = view;
    CGSize size = presentingView.frame.size;

    CGRect gridViewRect = self.optionsGridView.frame;
    gridViewRect.origin.y = (size.height - gridViewRect.size.height);;
    self.optionsGridView.frame = gridViewRect;
    
    self.frame = CGRectMake(0, 0, size.width, size.height);
    self.backgroundColor = [UIColor blackColor];
    self.alpha = 0.8;
    UILabel *labelOK = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, 320, 50)];
    labelOK.text = @"点击确定修改";
    labelOK.font = [UIFont systemFontOfSize:18.0];
    labelOK.textColor = [UIColor whiteColor];
    labelOK.textAlignment = NSTextAlignmentCenter;
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(optionSelectConfirm)]];
    [self addSubview:labelOK];
    
    [_optionsGridView reloadData];
    [presentingView addSubview:self];
    [presentingView addSubview:_optionsGridView];
    
}

- (NSMutableArray *)optionItemSelectedList {
    if (!_optionItemSelectedList) {
        _optionItemSelectedList = [NSMutableArray arrayWithCapacity:5];
    }
    return _optionItemSelectedList;
}

- (NSMutableArray *)optionItemList {
    if (!_optionItemList) {
        _optionItemList = [NSMutableArray arrayWithCapacity:5];
    }
    return _optionItemList;
}

- (void)addOptionItemTitle:(NSString *)title value:(NSString *)val {
    
    [self.optionItemList addObject:[[MCOptionSelectViewItem alloc] initWithTitle:title value:val]];
    
}

- (void)addOptionItem:(MCOptionSelectViewItem *)item {
    
    [self.optionItemList addObject:item];
}

- (IBAction)optionSelectConfirm {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(theMCOptionsSelectView:confirmList:)]) {
        [self.delegate theMCOptionsSelectView:self confirmList:_optionItemSelectedList];
    }
    
    [self.optionsGridView removeFromSuperview];
    [self removeFromSuperview];
}

- (void)setOptionItemCheckedIndex:(NSInteger)optionItemCheckedIndex {
    
    if (optionItemCheckedIndex >= self.optionItemList.count) {
        return;
    }

    [self.optionItemSelectedList addObject:self.optionItemList[optionItemCheckedIndex]];
    
    self.preSelectedIndexPath = [NSIndexPath indexPathForRow:optionItemCheckedIndex%_columns inSection:optionItemCheckedIndex/_columns];
}

#pragma mark - UICollectionView Delegate
//!行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellId = @"MCOptionSelectViewCellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    //NSArray *weekDaysTitles = @[ @[@"周一", @"周二", @"周三"], @[@"周四", @"周五", @"周六"], @[@"周日"]  ];
    NSMutableArray *cellTitles = [NSMutableArray arrayWithCapacity:3];
    
    for (NSInteger i=indexPath.row*self.columns; i<((indexPath.row+1)*self.columns) && i<self.optionItemList.count; i++) {
        
        [cellTitles addObject:self.optionItemList[i]];
    }
    
    [self addOptionItemButtons:cellTitles toCell:cell];
    return cell;
}

- (void)addOptionItemButtons:(NSArray *)optionList toCell:(UITableViewCell *)cell {
    
    CGFloat x = 0, y = 10;
    for (NSInteger i=0; i<optionList.count; i++) {
        
        MCOptionSelectViewItem *item = optionList[i];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [btn setTitle:item.title forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        //btn.titleLabel.text = btnTiltes[i];
        [btn setImage:[UIImage imageNamed:@"import_unchecked"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"import_checked"] forState:UIControlStateSelected];
        btn.userInteractionEnabled = YES;
        btn.frame = CGRectMake(x, y, 100, 20);
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        [cell.contentView addSubview:btn];

        x += 100;
    }
}

- (IBAction)btnClicked:(UIButton *)sender {
    
    CGRect btnRect = sender.frame;
    CGPoint p = [sender convertPoint:btnRect.origin toView:self.optionsGridView];
    NSIndexPath *indexPath = [self.optionsGridView indexPathForRowAtPoint:CGPointMake(0, p.y)];
    NSInteger optionItemIndex = indexPath.row * self.columns + sender.tag;
    
    if (self.optionItemList.count > optionItemIndex) {
        
        MCOptionSelectViewItem *item = [self.optionItemList objectAtIndex:optionItemIndex];
        item.checked = !item.checked;
        if (item.checked) {
            [self.optionItemSelectedList addObject:item];
        }else
            [self.optionItemSelectedList removeObject:item];
    }
    sender.selected = YES;
}



@end
